package com.baofu.paycore.service;

import com.baofu.paycore.biz.paycore.handler.PayStatusRepeatHandler;
import com.baofu.paycore.biz.paycore.handler.PayStatusRetryHandler;
import com.baofu.paycore.common.utils.ExceptionUtil;
import com.baofu.paycore.service.facade.PayStatusRetryFacade;
import com.baofu.paycore.service.facade.model.PayBaseResDTO;
import com.system.commons.result.Result;
import com.system.commons.utils.ParamValidate;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.slf4j.helpers.SystemMarker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>账务失败重发接口
 * </p>
 * User:ames wang  Date: 2017/6/29
 */
@Slf4j
@Service
public class PayStatusRetryService implements PayStatusRetryFacade {

    /**
     * 补发处理类
     */
    @Autowired
    private PayStatusRetryHandler payStatusRetryHandler;


    /**
     * 重试处理类
     */
    @Autowired
    private PayStatusRepeatHandler payStatusRepeatHandler;

    /**
     * 账务失败订单补发(充值，提现)
     * @param payNo             支付单号
     * @param serviceCode       服务编号
     * @param transLogId        日志ID
     * @return                  Result<PayBaseResDTO>
     */
    @Override
    public Result<PayBaseResDTO> retryAccountHandler(String payNo, String serviceCode, String transLogId) {
        MDC.put(SystemMarker.TRACE_LOG_ID, transLogId);
        Result<PayBaseResDTO> result = null;
        try {
            log.info("账务失败订单补发参数:订单号{},服务编码{}", payNo, serviceCode);
            //校验对象
            ParamValidate.validateStringNull(payNo, serviceCode);
            //补单
            String status = payStatusRetryHandler.retryAccountHandler(payNo, serviceCode);
            PayBaseResDTO payBaseResDTO = new PayBaseResDTO();
            payBaseResDTO.setStatus(status);
            result = new Result<>(payBaseResDTO);

        } catch (Exception e) {
            log.error("账务失败订单补发 异常:{}", e);
            result = ExceptionUtil.doExceptionService(e, PayBaseResDTO.class);
        } finally {
            log.info("账务失败订单补发 结果:{}", result);
            MDC.clear();
        }
        return result;
    }

    @Override
    public Result<PayBaseResDTO> dispatchAccountHandler(String payNo, String serviceCode, String transLogId) {
        MDC.put(SystemMarker.TRACE_LOG_ID, transLogId);
        Result<PayBaseResDTO> result = null;
        try {
            log.info("账务未知状态重试参数:订单号{},服务编码{}", payNo, serviceCode);
            //校验对象
            ParamValidate.validateStringNull(payNo, serviceCode);
            //补单
            String status = payStatusRepeatHandler.process(payNo, serviceCode);
            PayBaseResDTO payBaseResDTO = new PayBaseResDTO();
            payBaseResDTO.setStatus(status);
            result = new Result<>(payBaseResDTO);

        } catch (Exception e) {
            log.error("账务未知状态重试 异常:{}", e);
            result = ExceptionUtil.doExceptionService(e, PayBaseResDTO.class);
        } finally {
            log.info("账务未知状态重试 结果:{}", result);
            MDC.clear();
        }
        return result;
    }
}
